package com.tsing.cli.common.util;

import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;

/**
 * 文件格式检查工具类
 *
 * @author TheTsing
 */
public class FileCheckUtil {

    public static boolean checkImageSuffix(MultipartFile file) {
        return check(file, new String[]{".jpg", ".jpeg", ".png", ".gif", ".bmp"});
    }

    public static boolean checkOfficeSuffix(MultipartFile file) {
        return check(file, new String[]{".docx", ".doc", ".pdf", ".xlsx", ".xls", ".pptx", ".ppt"});
    }

    public static boolean checkVideoSuffix(MultipartFile file) {
        return check(file, new String[]{".mp4", ".rmvb", ".flv", ".swf", ".wmv", ".avi", ".mpg", ".mpeg", ".mov", ".ram", ".rm"});
    }

    private static boolean check(MultipartFile file, String[] fileTypes) {
        boolean flag = false;
        if (Objects.isNull(file.getOriginalFilename())) {
            String fileName = file.getOriginalFilename().toLowerCase();
            for (String fileType : fileTypes) {
                if (fileName.endsWith(fileType)) {
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }

}
